package com.knowif.backend.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.knowif.backend.service.UserCommentService;
import com.knowif.backend.service.UserOrderService;
import com.knowif.pojo.UserComment;
import com.knowif.pojo.UserHelpful;
import com.knowif.search.SearchCondition;
import com.knowif.system.model.RestBean;


@RestController
@RequestMapping("/wechat/userComment")
public class UserCommentController {
	
	
	@Autowired
	private UserCommentService userCommentService;
	
	@Autowired
	private UserOrderService userOrderService;
	
	/**
	 * @author: xingyao 
	 * @Description:  查看导师订单评价
	 * @param searchCondition
	 * @return
	 */
	@PostMapping("/selectComment")
	public RestBean<?> selectComment(@RequestBody SearchCondition searchCondition){
		if(searchCondition == null){
			return RestBean.buildError("请输入要查找的导师id");
		}
		try {
			return userOrderService.selectOrderComment(searchCondition);
		} catch (Exception e) {
			e.printStackTrace();
			return RestBean.buildError(RestBean.SYSTEMERROR);
		}
	}
	
	
	@PostMapping("/selectMoreComment")
	public RestBean<?> selectMoreComment(@RequestBody SearchCondition searchCondition){
		if(searchCondition == null){
			return RestBean.buildError("请输入查询条件");
		}
		try {
			return userCommentService.selectMoreComment(searchCondition);
		} catch (Exception e) {
			e.printStackTrace();
			return RestBean.buildError(RestBean.SYSTEMERROR);
		}
	}
	
	
	/**
	 * @author: xingyao 
	 * @Description:  删除用户评论
	 * @param userCommentId
	 * @return
	 */
	@PostMapping("/deleteComment")
	public RestBean<?> deleteComment(Integer userCommentId){
		if(userCommentId == null){
			return RestBean.buildError("请选择要删除的用户评论");
		}
		return userCommentService.deleteUserComment(userCommentId);
	}
	
	
	
	/**
	 * @author: xingyao 
	 * @Description:  保存用户评论
	 * @param userComment
	 * @return
	 */
	@PostMapping("/saveUserComment")
	public RestBean<?> saveUserComment(@RequestBody @Validated UserComment userComment){
		try{
			return userCommentService.saveUserComment(userComment);
		}catch(Exception e){
			e.printStackTrace();
			return RestBean.buildError(RestBean.SYSTEMERROR);
		}
	}
	
	
	@PostMapping("/isUsed")
	public RestBean<?> isUsed(@Validated UserHelpful userHelpful){
		if(userHelpful == null){
			return RestBean.buildError("点赞失败");
		}
		try{
			return userCommentService.isUsed(userHelpful);
		}catch(Exception e){
			return RestBean.buildError(RestBean.SYSTEMERROR);
		}
	}

}
